//
//  BaseTabbarController.swift
//  Kool Car
//
//  Created by huangweiqiang on 2021/12/14.
//

import UIKit

class BaseTabbarController: ESTabBarController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
     
        //选定义tabbarItems
        let buyCarItem = ESTabBarItem.init(ESTabBarItemContentView())
        buyCarItem.title = MMLocalizedString("tabbar_buy_cars", comment: "我要买车")
        buyCarItem.tag = 0
        buyCarItem.image = UIImage.init(named: "icon_tabbar_buy_cars")
        buyCarItem.selectedImage = UIImage.init(named: "icon_tabbar_buy_cars_pre")
        
        let overseasItem = ESTabBarItem.init()
        overseasItem.tag = 1
        overseasItem.title = MMLocalizedString("sea_car_title", comment: "海外车辆")
        overseasItem.image = UIImage.init(named: "tabbar_sea")
        overseasItem.selectedImage = UIImage.init(named: "tabbar_sea_pre")
        
        let sellCarItem = ESTabBarItem.init(ESTabBarItemContentView())
        sellCarItem.title = MMLocalizedString("tabbar_sell_car", comment: "我要卖车")
        sellCarItem.tag = 2
        sellCarItem.image = UIImage.init(named: "icon_tabbar_sell_car")
        sellCarItem.selectedImage = UIImage.init(named: "icon_tabbar_sell_car_pre")
        
        let maintenanceItem = ESTabBarItem.init(ESTabBarItemContentView())
        maintenanceItem.tag = 3
        maintenanceItem.title = MMLocalizedString("tabbar_maintenance", comment: "维修保养")
        maintenanceItem.image = UIImage.init(named: "icon_tabbar_maintenance")
        maintenanceItem.selectedImage = UIImage.init(named: "icon_tabbar_maintenance_pre")
        
        let carInsuranceItem = ESTabBarItem.init(ESTabBarItemContentView())
        carInsuranceItem.tag = 4
        carInsuranceItem.title = MMLocalizedString("tabbar_car_insurance", comment: "车保")
        carInsuranceItem.image = UIImage.init(named: "icon_tabbar_carinsurance")
        carInsuranceItem.selectedImage = UIImage.init(named: "icon_tabbar_carinsurance_pre")
        
        let newsItem = ESTabBarItem.init(ESTabBarItemContentView())
        newsItem.title = MMLocalizedString("tabbar_news", comment: "资讯")
        newsItem.tag = 5
        newsItem.image = UIImage.init(named: "icon_tabbar_news")
        newsItem.selectedImage = UIImage.init(named: "icon_tabbar_news_pre")
        
        //自定义tabbarItems（6个tabbar）
        if let tabbar = self.tabBar as? ESTabBar {
            tabbar.vItems = [buyCarItem, overseasItem, sellCarItem, maintenanceItem, carInsuranceItem, newsItem]
            tabbar.backgroundColor = Theme.whiteColor
        }
        
        //控制器
        //我要买车
        let buyCarsVC = BuyCarsViewController()
        let buyCarsNavi = BaseNavigationController.init(rootViewController: buyCarsVC)
        buyCarsNavi.tabBarItem = buyCarItem
        self.addChildViewController(buyCarsNavi)
        
        //海外车
        let overseasVC = OverSeaViewController()
        let overseasNavi = BaseNavigationController.init(rootViewController: overseasVC)
        overseasNavi.tabBarItem = overseasItem
        self.addChildViewController(overseasNavi)
        
        //我要卖车
        let sellCarVC = SellCarViewController()
        let sellCarsNavi = BaseNavigationController.init(rootViewController: sellCarVC)
        sellCarsNavi.tabBarItem = sellCarItem
        self.addChildViewController(sellCarsNavi)
        
        //维修保养
        let maintenanceVC = MaintenanceViewController()
        
        let maintenanceNavi = BaseNavigationController.init(rootViewController: maintenanceVC)
        maintenanceNavi.tabBarItem = maintenanceItem
        self.addChildViewController(maintenanceNavi)
        
        //车保
        let carInsuranceVC = CarInsuranceViewController()
        let carInsuranceNavi = BaseNavigationController.init(rootViewController: carInsuranceVC)
        carInsuranceNavi.tabBarItem = carInsuranceItem
        self.addChildViewController(carInsuranceNavi)
        
        //资讯
        let newsVC = NewsViewController()
        let newsNavi = BaseNavigationController.init(rootViewController: newsVC)
        newsNavi.tabBarItem = newsItem
        self.addChildViewController(newsNavi)
        self.selectedIndex = 0
        self.delegate = self
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
    }
    
    ///去掉tabbar顶部的黑线
   private func initWithImage(color:UIColor)->UIImage{
        let rect = CGRect(x: 0,y: 0,width: 1,height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
    
    /// 这里是对iphonex类型的手机适配 底部的黑色横条 上滑一次关闭
    ///
    /// - Returns:
    override func preferredScreenEdgesDeferringSystemGestures() -> UIRectEdge {
        return UIRectEdge.all
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension BaseTabbarController: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        let item = viewController.tabBarItem
        if item?.tag == 2 {
            //点击服务
            if !(UserModule.shared.isLogined ?? false) {
                XTManageCenter.shared.jumpToLoginView()
                return false
            }
        }
        return true
    }
}
